<?php

/**
 * character actions.
 *
 * @package    s-test
 * @subpackage character
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class characterActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
        $this->ktd_characters = KTDCharacter::getOrderedCharacters();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->ktd_character = Doctrine::getTable('KTDCharacter')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->ktd_character);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new KTDCharacterForm();
    
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $ktd_character = new KTDCharacter();
    $ktd_character->setKTDPlayer($this->getUser()->getGuardUser());
   
    $this->form = new KTDCharacterForm($ktd_character);
    $this->processForm($request, $this->form);
    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($ktd_character = Doctrine::getTable('KTDCharacter')->find(array($request->getParameter('id'))), sprintf('Object ktd_character does not exist (%s).', $request->getParameter('id')));
    $this->form = new KTDEditCharacterForm($ktd_character);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($ktd_character = Doctrine::getTable('KTDCharacter')->find(array($request->getParameter('id'))), sprintf('Object ktd_character does not exist (%s).', $request->getParameter('id')));
    $this->form = new KTDEditCharacterForm($ktd_character);
    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $this->forward404Unless($ktd_character = Doctrine::getTable('KTDCharacter')->find(array($request->getParameter('id'))), sprintf('Object ktd_character does not exist (%s).', $request->getParameter('id')));
    $this->forward404Unless($this->getUser()->hasRelPermission("delete_character", $ktd_character->getKTDPlayer()), "Vous ne pouvez pas supprimer ce personnage !");

    $ktd_character->delete();
    $this->redirect('character/index');
  }
  public function executeValidate(sfWebRequest $request){

      $this->forward404Unless($ktd_character = Doctrine::getTable('KTDCharacter')->find(array($request->getParameter('id'))), sprintf('Object ktd_character does not exist (%s).', $request->getParameter('id')));
      $todo = !$ktd_character->getIsValid();
      $this->forward404Unless($this->getUser()->hasPermission("validate_character"), "Vous ne pouvez pas valider de fiches !");

      if($todo){
          $ktd_character->setIsValid(true);
          KTDEventManager::handleFiche("onValidation", array("character" => $ktd_character));
          $this->getUser()->setFlash("validate_character", sprintf("Le personnage %s a été validé avec succés !", $ktd_character->getPseudo()));
      }
      else{
          $ktd_character->setIsValid(false);
          KTDEventManager::handleFiche("onDevalidation", array("character" => $ktd_character));
          $this->getUser()->setFlash("validate_character", sprintf("Le personnage %s a été dévalidé avec succés !", $ktd_character->getPseudo()));
      }
      $ktd_character->save();
      $ktd_character->getFiche()->setUpdatedAt(time());
      $ktd_character->getFiche()->save();
      $this->redirect($this->generateUrl("show_fiche", $ktd_character));
  }
  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $ktd_character = $form->save();
      
      $ktd_character->getKTDPlayer()->setActualCharacter($ktd_character);
      $this->redirect($this->generateUrl("show_fiche", $ktd_character));
    }
  }
}
